package com.ndp.fb.walle.business.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.model.business.TimeRange;
import com.ndp.fb.model.business.vo.admanage.param.GroupByAdSetParam;
import com.ndp.fb.model.business.vo.admanage.param.GroupByDayParam;
import com.ndp.fb.mongo.api.AdGroupMongoService;
import com.ndp.fb.util.DateUtil;
import com.ndp.fb.walle.business.AdGroupService;
import com.ndp.fb.walle.business.AdManageService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dangw on 2016/1/22.
 */
@Component
public class AdManageServiceImpl implements AdManageService {
    @Reference
    AdGroupMongoService adGroupMongoService;

    @Autowired
    AdGroupService adGroupService;

    @Override
    public Map<String, Object> groupByAdset(GroupByAdSetParam groupByAdSetParam) {
        return adGroupMongoService.groupByAdset(groupByAdSetParam);
    }

    @Override
    public Map<String, Object> graphByDay(GroupByDayParam groupByDayParam) {
        Map<String, Object> dataMap = new LinkedHashMap<>();
        try {
            TimeRange timeRange = groupByDayParam.getStatsTimeRange();
            Long id = groupByDayParam.getId();

            Date sinceDate = DateUtils.parseDate(timeRange.getSince(), "yyyy-MM-dd");
            Date untilDate = DateUtils.parseDate(timeRange.getUntil(), "yyyy-MM-dd");
            long totalDays = DateUtil.dayDiff(sinceDate, untilDate) + 1;

            List<String> returnStatsFields = groupByDayParam.getReturnStatsFields();
            // 因为要返回的是returnStatsFields中包含的所有字段的有序列表，并且个数要与给定的天数一致，因此先要初始化一个全部为0的返回结果
            Map<String, Map<String, Object>> returnFieldsMap = new HashMap<>();
            for (String returnStatsField : returnStatsFields) {
                returnFieldsMap.put(returnStatsField, new LinkedHashMap<>());
            }
            for (int i = 0; i < totalDays; i++) {
                Date date = DateUtil.addDateByTypeAndOffset(sinceDate, Calendar.DATE, i);
                String day = DateUtil.dateToString(date, "yyyy-MM-dd");
                for (Map<String, Object> returnFieldMap : returnFieldsMap.values()) {
                    returnFieldMap.put(day, 0);
                }
            }
            // 从Mongodb中按天查询，返回的是一个列表，类似于[{"day":"2015-12-01","clicks":20,"spent":90},{"day":"2015-12-02","clicks":30,"spent":120}]，需要通过这些真实的值覆盖之前生成的全为0的返回结果
            List<Map<String, Object>> groupByDayResult = adGroupMongoService.groupByDay(groupByDayParam);
            for (Map<String, Object> statsMap : groupByDayResult) {
                for (String returnStatsField : returnStatsFields) {
                    Object returnValue = statsMap.get(returnStatsField);
                    returnFieldsMap.get(returnStatsField).put(statsMap.get("day").toString(), returnValue);
                }
            }

            // 最终返回结果
            for (String returnStatsField : returnStatsFields) {
                dataMap.put(returnStatsField, returnFieldsMap.get(returnStatsField).values());
            }
            // TODO 由于不是所有的广告都能查询到单价，因此暂时不显示单价，接口返回常量零，避免前端出错。
//            AdForMongo ad = adGroupMongoService.findAdSetByAdIdOrAdSetId(id);
//            VpiInfo vpiFromCache = adGroupService.findVpiFromCache(id);
//            if (null != vpiFromCache && null != vpiFromCache.getVpi()) {
//                dataMap.put("vpi", vpiFromCache.getVpi());
//            } else {
//                dataMap.put("vpi", 0);
//            }
            dataMap.put("vpi", 0);
        } finally {
            return dataMap;
        }
    }
}
